<template>
  <div class="login-container">
    <div class="login-box">
      <div class="login-header">
        <h2 class="title">欢迎登录</h2>
        <p class="subtitle">OA办公系统</p>
      </div>
      <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on">
        <el-form-item prop="username">
          <el-input ref="username" v-model="loginForm.username" placeholder="请输入用户名" name="username" type="text"
            prefix-icon="el-icon-user" tabindex="1" auto-complete="on" />
        </el-form-item>

        <el-form-item prop="password">
          <el-input :key="passwordType" ref="password" v-model="loginForm.password" :type="passwordType"
            placeholder="请输入密码" name="password" prefix-icon="el-icon-lock" tabindex="2" auto-complete="on"
            @keyup.enter.native="handleLogin">
            <template slot="suffix">
              <i :class="['password-eye', passwordType === 'password' ? 'el-icon-view' : 'el-icon-hide']"
                @click="showPwd" />
            </template>
          </el-input>
        </el-form-item>

        <el-form-item>
          <el-button :loading="loading" type="primary" class="login-button" @click.native.prevent="handleLogin">
            {{ loading ? '登录中...' : '登录' }}
          </el-button>
        </el-form-item>

        <div class="login-tips">
          <p>测试账号：admin</p>
          <p>测试密码：123456</p>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script>
import { validUsername } from '@/utils/validate'
import { useUserStore } from '@/store/modules/user'

export default {
  name: 'Login',
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!validUsername(value)) {
        callback(new Error('请输入正确的用户名'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('密码不能少于6位'))
      } else {
        callback()
      }
    }
    return {
      loginForm: {
        username: 'admin',
        password: '123456'
      },
      loginRules: {
        username: [{ required: true, trigger: 'blur', validator: validateUsername }],
        password: [{ required: true, trigger: 'blur', validator: validatePassword }]
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  methods: {
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          const userStore = useUserStore()
          userStore.login(this.loginForm).then(() => {
            this.$router.push({ path: this.redirect || '/' })
            this.loading = false
          }).catch(() => {
            this.loading = false
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss">
.login-container {
  .el-input {
    --el-input-bg-color: transparent;
    --el-input-border-color: #dcdfe6;
    --el-input-hover-border-color: #c0c4cc;
    --el-input-focus-border-color: var(--el-color-primary);

    .el-input__wrapper {
      background-color: rgba(255, 255, 255, 0.9);
      box-shadow: none;
      transition: all 0.3s;

      &.is-focus {
        background-color: #ffffff;
      }
    }

    input {
      height: 48px;
      color: #606266;
      caret-color: var(--el-color-primary);

      &:-webkit-autofill {
        box-shadow: 0 0 0 1000px #fff inset;
      }
    }
  }

  .el-form-item {
    margin-bottom: 25px;
  }
}
</style>

<style lang="scss" scoped>
.login-container {
  height: 100vh;
  width: 100%;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  display: flex;
  justify-content: center;
  align-items: center;
  overflow: hidden;

  .login-box {
    width: 420px;
    padding: 40px;
    background: rgba(255, 255, 255, 0.95);
    border-radius: 12px;
    box-shadow: 0 8px 20px rgba(0, 0, 0, 0.1);
    backdrop-filter: blur(10px);
    animation: fadeIn 0.6s ease-out;
  }

  .login-header {
    text-align: center;
    margin-bottom: 40px;

    .title {
      font-size: 28px;
      color: #303133;
      margin-bottom: 8px;
      font-weight: 600;
    }

    .subtitle {
      font-size: 16px;
      color: #606266;
      margin: 0;
    }
  }

  .login-form {
    .password-eye {
      font-size: 16px;
      color: #909399;
      cursor: pointer;
      transition: color 0.2s;

      &:hover {
        color: #606266;
      }
    }

    .login-button {
      width: 100%;
      height: 48px;
      font-size: 16px;
      margin-top: 10px;
    }
  }

  .login-tips {
    margin-top: 20px;
    text-align: center;
    color: #909399;
    font-size: 14px;

    p {
      margin: 5px 0;
    }
  }
}

@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(20px);
  }

  to {
    opacity: 1;
    transform: translateY(0);
  }
}
</style>